package Screens;

import Controllers.HdEquipamentoJpaController;
import Controllers.HdTipoEquipamentoJpaController;
import Entities.HdEquipamento;
import Entities.HdTipoEquipamento;
import Util.Constantes;
import Util.EMFConection;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Ferrari
 */
public class CadastroEquipamento extends javax.swing.JFrame {

    /**
     * Creates new form CadastroEquipamento
     */
    public CadastroEquipamento() {
        initComponents();
        
        HdTipoEquipamentoJpaController tipoEquipamentoJpaController = new HdTipoEquipamentoJpaController(EMFConection.getInstance());
        List<HdTipoEquipamento> list = tipoEquipamentoJpaController.findHdTipoEquipamentoEntities();
        for (HdTipoEquipamento tipoEquipamento : list) {
            cbTipoEquipamento.addItem(tipoEquipamento.getDeTipoEquipamento());
        }
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btCancelar = new javax.swing.JButton();
        btCadastrar = new javax.swing.JButton();
        cbTipoEquipamento = new javax.swing.JComboBox();
        lbTipoEquipamento = new javax.swing.JLabel();
        tfDeEquipamento1 = new javax.swing.JTextField();
        lbDeEquipamento1 = new javax.swing.JLabel();
        tfDeEquipamento2 = new javax.swing.JTextField();
        lbDeEquipamento2 = new javax.swing.JLabel();
        lbDeEquipamento3 = new javax.swing.JLabel();
        tfDeEquipamento3 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Help Desk");
        setResizable(false);

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btCadastrar.setText("Cadastrar");
        btCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCadastrarActionPerformed(evt);
            }
        });

        lbTipoEquipamento.setForeground(new java.awt.Color(1, 1, 1));
        lbTipoEquipamento.setText("Tipo de equipamento*:");

        lbDeEquipamento1.setText("Descrição  de equipamento 1*:");

        lbDeEquipamento2.setText("Descrição  de equipamento 2:");

        lbDeEquipamento3.setText("Descrição  de equipamento 3:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btCadastrar)
                        .addGap(18, 18, 18)
                        .addComponent(btCancelar))
                    .addComponent(tfDeEquipamento2, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                    .addComponent(lbDeEquipamento2)
                    .addComponent(tfDeEquipamento1, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                    .addComponent(lbDeEquipamento1)
                    .addComponent(tfDeEquipamento3, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                    .addComponent(lbDeEquipamento3)
                    .addComponent(lbTipoEquipamento)
                    .addComponent(cbTipoEquipamento, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbTipoEquipamento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbTipoEquipamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lbDeEquipamento1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfDeEquipamento1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lbDeEquipamento2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfDeEquipamento2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lbDeEquipamento3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfDeEquipamento3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCadastrar)
                    .addComponent(btCancelar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-358)/2, (screenSize.height-323)/2, 358, 323);
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCadastrarActionPerformed
        HdEquipamentoJpaController hdEquipamentoJpaController = new HdEquipamentoJpaController(EMFConection.getInstance());
        HdTipoEquipamentoJpaController controller = new HdTipoEquipamentoJpaController(EMFConection.getInstance());
        
        HdEquipamento hdEquipamento = new HdEquipamento();
        if(!tfDeEquipamento1.getText().isEmpty()){
            hdEquipamento.setCoTipoEquipamento(controller.findHdTipoEquipamento(cbTipoEquipamento.getSelectedIndex()+1));
            hdEquipamento.setDeEquipamento(tfDeEquipamento1.getText());
            hdEquipamento.setDeEquipamento2(tfDeEquipamento2.getText());
            hdEquipamento.setDeEquipamento3(tfDeEquipamento3.getText());
            try{
                hdEquipamentoJpaController.create(hdEquipamento);
                JOptionPane.showMessageDialog(null, Constantes.DADOS_INSERIDOS, Constantes.SUCESSO, JOptionPane.INFORMATION_MESSAGE);
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, Constantes.ERRO_BANCO, Constantes.ERRO, JOptionPane.ERROR_MESSAGE);
            }
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null, Constantes.DESCRICAO_EM_BRANCO, Constantes.ERRO, JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btCadastrarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCadastrar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JComboBox cbTipoEquipamento;
    private javax.swing.JLabel lbDeEquipamento1;
    private javax.swing.JLabel lbDeEquipamento2;
    private javax.swing.JLabel lbDeEquipamento3;
    private javax.swing.JLabel lbTipoEquipamento;
    private javax.swing.JTextField tfDeEquipamento1;
    private javax.swing.JTextField tfDeEquipamento2;
    private javax.swing.JTextField tfDeEquipamento3;
    // End of variables declaration//GEN-END:variables
}
